using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace SpeedSearch.Controls
{

    public partial class DirectoryDialog : UserControl
    {

        public new event EventHandler TextChanged;

        public DirectoryDialog()
        {
            InitializeComponent();
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            if (this.Height != txtDirectory.Height)
                this.Height = txtDirectory.Height;
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            using (FolderBrowserDialog f = new FolderBrowserDialog())
            {
                f.ShowNewFolderButton = true;
                f.SelectedPath = System.Environment.CurrentDirectory;
                if (!string.IsNullOrEmpty(txtDirectory.Text))
                    f.SelectedPath = txtDirectory.Text;
                if (f.ShowDialog() == DialogResult.OK)
                    txtDirectory.Text = f.SelectedPath;
                OnTextChanged(EventArgs.Empty);
            }
        }

        public string Directory
        {
            get { return txtDirectory.Text; }
            set { txtDirectory.Text = value; }
        }

        private void txtDirectory_TextChanged(object sender, EventArgs e)
        {
            if (TextChanged != null)
                TextChanged(this, e);
        }

        public bool IsValid
        {
            get
            {
                if (!string.IsNullOrEmpty(txtDirectory.Text))
                    return System.IO.Directory.Exists(txtDirectory.Text);
                else
                    return false;

            }
        }

    }
}
